package com.doordu.soa.service.data.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.doordu.soa.service.comm.ResultCode;
import com.doordu.soa.service.comm.ResultData;
import com.doordu.soa.service.data.service.ParkingDeviceService;
import com.doordu.soa.service.data.vo.ParkingDeviceVo;
import com.doordu.soa.service.data.vo.RequestVo;
import com.doordu.soa.service.web.annotation.NotLogin;
import com.doordu.soa.service.web.annotation.Ver;
import com.doordu.soa.service.web.utils.BaseController;

import file.GZIPUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@Api(tags = "ParkingDevice", description = "停车场设备信息接口")
@RestController
@RequestMapping("/parking/device")
public class ParkingDeviceController extends BaseController {
	
	private final Logger logger = LoggerFactory.getLogger(ParkingDeviceController.class);
	
	@Autowired
	private ParkingDeviceService parkingDeviceService;

	@ApiOperation(value = "上报停车设备数据", notes = "上报停车设备数据")
	@ApiResponses({ @ApiResponse(code = 200, message = "处理成功") })
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ver", value = "接口版本", paramType = "path", required = true, dataType = "String", defaultValue = "v1"),
			@ApiImplicitParam(name = "data", value = "请求数据体", paramType = "query", required = true, dataType = "String"), })
	@RequestMapping(path = "/{ver}", method = RequestMethod.POST)
	@Ver("1")
	public ResponseEntity<Object> createParkingDevice(RequestVo requestVo, HttpServletRequest request) {
		if(requestVo.getGzipFlag().intValue()==1){
			try{
				requestVo.setData(GZIPUtils.uncompressToString(GZIPUtils.compress(requestVo.getData())));
			}catch(Exception e){
				logger.error("",e);
			}
		}
		if(StringUtils.isBlank(requestVo.getData())){
			return checkParamError("请求体data数据,gzip压缩有误");
		}
		requestVo.setCompanyId(getCompanyId(request));
		ResultData<Object> resultData = new ResultData<Object>(ResultCode.RESPONSE_SUCCESS);
		try{
			parkingDeviceService.insertData(resultData,requestVo,"/parking/watchhouse/v1", "上报岗亭数据");
			if(null!=resultData&&resultData.getCode().equals(ResultCode.RESPONSE_SUCCESS)){
				return okResponse(resultData.getData(),resultData.getMsg(),resultData.getDesc());
			}
			return resultDataError(resultData);
		}catch(Exception e){
			logger.error("",e);
			return error(ResultCode.RESPONSE_INTRANET_ERROR,e.getMessage());
		}
		
	}

	@ApiOperation(value = "上报参数data参数校验-示例", notes = "上报参数data参数校验-示例")
	@ApiResponses({ @ApiResponse(code = 200, message = "处理成功") })
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ver", value = "接口版本", paramType = "path", required = true, dataType = "String", defaultValue = "v1") })
	@RequestMapping(path = "/demo/{ver}", method = RequestMethod.POST)
	@Ver("1")
	@NotLogin
	public ResponseEntity<Object> createParkingDeviceDemo(ParkingDeviceVo parkingDeviceVo) {
		String errorMsg = checkData(parkingDeviceVo);
		if (StringUtils.isNotBlank(errorMsg)) {
			return checkParamError(errorMsg);
		}
		return ok("操作成功");
	}

}
